package com.itdd.service.impl;

import com.itdd.mapper.BookMapper;
import com.itdd.pojo.Book;
import com.itdd.service.BookService;
import com.itdd.service.utils.GetSqlSession;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Scanner;

public class BookServiceImpl implements BookService {

    @Override
    public void addBook() {
        SqlSession sqlSession = GetSqlSession.getSqlSession();
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
//定义一个输入
        Scanner Sca = new Scanner(System.in);
        do {
            //一个全部书的集合
            List<Book> allBook = bookMapper.selectAll();
            System.out.println("这是当前书库:");
            for (int i = 0; i < allBook.size(); i++) {
                System.out.println(allBook.get(i));
            }
            System.out.println("请依次输入你要添加的书的信息:书名，书类，书的数量");
            Book aBook = new Book();
            aBook.setBookName(Sca.next());
            aBook.setBookType(Sca.next());
            aBook.setBookNum(Sca.nextInt());
            bookMapper.addBook(aBook);
            System.out.println("添加成功！");
            System.out.println("如果想退出，请按0");
        } while (Sca.nextInt() != 0);
        sqlSession.close();
    }

    @Override
    public void deleteBook() {
        SqlSession sqlSession = GetSqlSession.getSqlSession();
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
//定义一个输入
        Scanner Sca = new Scanner(System.in);
        do {
            //一个全部书的集合
            List<Book> allBook = bookMapper.selectAll();
            System.out.println("这是当前书库:");
            for (int i = 0; i < allBook.size(); i++) {
                System.out.println(allBook.get(i));
            }
            System.out.println("请输入你要删除的书籍的书号\n想退出按0");
            int judge = Sca.nextInt();
            if (judge == 0) break;
            bookMapper.deleteByBookId(judge);
            System.out.println("删除成功！");
            System.out.println("如果想退出，请按0");
        } while (Sca.nextInt() != 0);
        sqlSession.close();
    }


}
